package com.msoe.client.panels.eventhandlers;

import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.msoe.client.MSOERServerAsync;
import com.msoe.client.Server;
import com.msoe.client.panels.MessageBox;
import com.msoe.client.panels.PanelMap;
import com.msoe.client.panels.Register;

public class RegisterEventHandler implements ClickListener, KeyboardListener{
	
	private Register reg;
	
	public RegisterEventHandler(Register register){
		reg = register;
	}
	
	public void onClick(Widget sender)
	{

		HashMap<String, String> fields = reg.getFields();
		
		if(sender instanceof Button){

			//if they pressed cancel then return to login screen
			if(((Button)sender).getText().equals("Cancel")){
				PanelMap.showPanel(reg, PanelMap.getPanel("Login"));
			}
			//else attempt to register them
			else if(((Button)sender).getText().equals("Submit"))
			{
				if( validateFields(fields) ){
					MSOERServerAsync server = Server.getInstance().getServer();
	
					AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>()
					{
						public void onSuccess(Boolean result)
						{
							if(result == true) // Register successful
							{
								MessageBox.showMessageDialog("Registration", "Registration was successful. An email has been sent to the address you provided.\nPlease login using the password given in the email.");
							}
							else
							{
								MessageBox.showMessageDialog("Registration", "Registration was not successful. Please try again.");
							}
						}
	
						public void onFailure(Throwable ex)
						{
							Window.alert(ex.getMessage());
						}
					};
	
					server.addUser(fields.get("username"), fields.get("fname"), fields.get("lname"), fields.get("email"), callback);
				}
			}
		}
	}
	
	private boolean validateFields(HashMap<String, String> fields){
		boolean retVal = true;
		String failMsg = "";
		
		//user name must be at least 4 characters and can contain numbers or letters
		if(!fields.get("username").matches("[A-Z,a-z,0-9]{4,}")){
			failMsg += "Username must be at least 4 letters or numbers.\n";
			retVal = false;
		}
		
		//first and last name must contain one or more LETTERS
		if(!fields.get("fname").matches("[A-Z,a-z]+")){
			failMsg += "First name must contain one or more letters.\n";
			retVal = false;
		}
		
		if(!fields.get("lname").matches("[A-Z,a-z]+")){
			failMsg += "Last name must contain one or more letters.\n";
			retVal = false;
		}
		
		//email must be in the msoe domain
		if(!fields.get("email").matches("^[A-Z,a-z,0-9,.]+@msoe.edu$")){
			failMsg += "Email must be from the msoe domain.\n";
			retVal = false;
		}
		
		if(!failMsg.equals("")){
			MessageBox.showMessageDialog("Registration", failMsg);
		}
		
		return retVal;
	}

	public void onKeyDown(Widget sender, char keyCode, int modifiers) {
		
	}

	public void onKeyPress(Widget sender, char keyCode, int modifiers) {
		
		
	}

	/**
	 * parses the username as the part of the msoe email address before the
	 * '@' character and puts the username in the username field
	 */
	public void onKeyUp(Widget sender, char keyCode, int modifiers) {
		
		if(sender instanceof TextBox){
			
			TextBox userBox = reg.getUserNameBox();
			TextBox emailBox = (TextBox) sender;
			
			if(emailBox.getText().indexOf("@") == -1){
				
				userBox.setText(emailBox.getText());
				
			}else{
				
				String username = emailBox.getText().substring(0,emailBox.getText().indexOf("@"));
			
				userBox.setText(username);
			}
		}
		
	}
}
